package tv.youxing.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.ShareUtils;

/**
 * Created by Administrator on 2017/6/29.
 */

public class GuideActivity extends AppCompatActivity {


    @BindView(R.id.vp)
    ViewPager mViewPager;


    List<View> imgs = new ArrayList<>();

    private boolean isLastPage = false;
    private boolean isDragPage = false;
    private boolean canJumpPage = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_activity);
        ButterKnife.bind(this);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ImageView img1 = new ImageView(this);
        img1.setScaleType(ImageView.ScaleType.FIT_XY);
        img1.setBackgroundResource(R.drawable.guide1);

        ImageView img2 = new ImageView(this);
        img2.setScaleType(ImageView.ScaleType.FIT_XY);
        img2.setBackgroundResource(R.drawable.guide2);

        ImageView img3 = new ImageView(this);
        img3.setScaleType(ImageView.ScaleType.FIT_XY);
        img3.setBackgroundResource(R.drawable.guide3);

        imgs.add(img1);
        imgs.add(img2);
        imgs.add(img3);


        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imgs.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {

                container.addView(imgs.get(position));
                return imgs.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView(imgs.get(position));
            }

            @Override
            public int getItemPosition(Object object) {

                return super.getItemPosition(object);
            }
        });

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (isLastPage && isDragPage && positionOffsetPixels == 0){   //当前页是最后一页，并且是拖动状态，并且像素偏移量为0
                    if (canJumpPage){
                        canJumpPage = false;
                        JumpToNext();
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {
                isLastPage = position == imgs.size()-1;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                isDragPage = state == 1;
            }
        });
    }


    /**
     * viewpager滑动到最后一页做跳转逻辑
     */
    private void JumpToNext() {
        startActivity(new Intent(this, VideoWelcomeActivity.class));
        ShareUtils.putBoolean(this, AppConst.ISGUIDE,true);
        finish();
    }

    public void start(View v) {
        startActivity(new Intent(this, VideoWelcomeActivity.class));
        ShareUtils.putBoolean(this, AppConst.ISGUIDE,true);
        finish();
    }
}
